/*
 * @Author: YeMiao 
 * @Date: 2018-02-24 16:45:59 
 * @Last Modified by: YeMiao
 * @Last Modified time: 2018-03-15 15:39:51
 */
import { Component, OnInit } from '@angular/core';
import { UserService } from './user.service';
import { UploadFile, NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  name = sessionStorage.getItem('name')?sessionStorage.getItem('name'):sessionStorage.getItem('user');
  logCount = 0;
  loginAmount = 0;
  onTime = sessionStorage.getItem('onTime');
  headUrl = 'url('+'/assets/head.jpg'+')';
  isHeadShow = false;
  loading = false;
  avatarUrl: string;

  constructor(
    private _userService: UserService,
    private _message: NzMessageService
  ) {}

  ngOnInit() {
    this._userService.getUser().subscribe(res => {
      if(res['code'] == 1){
        let data = res['data'];
        this.logCount = data['log_count'];
        this.loginAmount = data['login_amount'];
      }
    })
  } 

  headShow(sign: number) {
    if(sign == 1){
      this.isHeadShow = true;
    }else{
      this.isHeadShow = false;
    }
  }

  beforeUpload = (file: File) => {
    const isJPG = file.type === 'image/jpeg';
    if (!isJPG) {
      this._message.error('请上传jpg图片');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
      this._message.error('图片不能大于2MB');
    }
    return isJPG && isLt2M;
  }

  private getBase64(img: File, callback: (img: any) => void) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
  }

  handleChange(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      
      return;
    }
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      this.getBase64(info.file.originFileObj, (img: any) => {
        console.log(img);
      });
    }
  }

}
